package com.webc.framework.bean;

import java.lang.reflect.Method;
import java.util.Arrays;
import static com.webc.framework.bean.MappingContainer.MAPPING_ANNOTATION_CLASS;

/**
 * @author hejianglong
 * @date 2018/9/7.
 */
public class RequestHandler {

    private Method method;

    private Object controllerBean;

    private Class<?> mappingClass;

    public RequestHandler(Method method, Object controllerBean, Class mappingClass) {
        this.method = method;
        this.controllerBean = controllerBean;
        boolean containMappingClass = Arrays.asList(MAPPING_ANNOTATION_CLASS).contains(mappingClass);
        if (!containMappingClass) {
            throw new RuntimeException("mapping handler mappingClass error: " + mappingClass);
        }
        this.mappingClass = mappingClass;
    }

    public void checkMethod(String requestMethod) {
        String className = this.mappingClass.getName();
        String annotation = className.substring(className.lastIndexOf(".") + 1, className.length());
        // 请求方式错误
        if(!annotation.toUpperCase().contains(requestMethod.toUpperCase())) {
            throw new RuntimeException("请求方法类型错误，请检查: " + annotation);
        }
    }

    public Method getMethod() {
        return method;
    }

    public void setMethod(Method method) {
        this.method = method;
    }

    public Object getControllerBean() {
        return controllerBean;
    }

    public void setControllerBean(Object controllerBean) {
        this.controllerBean = controllerBean;
    }

    public Class<?> getMappingClass() {
        return mappingClass;
    }

    public void setMappingClass(Class<?> mappingClass) {
        this.mappingClass = mappingClass;
    }
}
